#include "EFileInfo.h"
#include "DefineCommon.h"
#include <iterator>
#include <string/pystring.h>

#ifdef WIN32
#include <Windows.h>
#include <windows.h>  
#include <ctime>  
#include <sstream>
#else
#include <sys/stat.h>
#include <time.h>
#endif

  
#ifdef WIN32
EString EFileInfo_getModifiedTimeStr(const char* path) 
{  
    EString result;
    // const char *path = "your_file_path_here";  
    HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  
  
    if (hFile == INVALID_HANDLE_VALUE) 
    {  
        // std::cerr << "Unable to open " << path << std::endl;  
        ELOG_ERROR("Unable to open %s", path);

        return result;  
    }  
  
    FILETIME ftCreate, ftAccess, ftWrite;  
    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) {  
        std::cerr << "Unable to get file time for " << path << std::endl;  
        CloseHandle(hFile);  
        return result;  
    }  
  
    // 将FILETIME转换为SYSTEMTIME，以便更容易地转换为本地时间  
    SYSTEMTIME stUTC, stLocal;  
    FileTimeToSystemTime(&ftWrite, &stUTC);  
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);  
  
    // 将SYSTEMTIME转换为time_t  
    struct tm timeinfo = {0};  
    timeinfo.tm_year = stLocal.wYear - 1900;  
    timeinfo.tm_mon = stLocal.wMonth - 1;  
    timeinfo.tm_mday = stLocal.wDay;  
    timeinfo.tm_hour = stLocal.wHour;  
    timeinfo.tm_min = stLocal.wMinute;  
    timeinfo.tm_sec = stLocal.wSecond;  
    timeinfo.tm_isdst = -1; // 让mktime确定夏令时  
  
    std::time_t mtime = mktime(&timeinfo);  

    char buffer[80]; // 用于存储转换后的时间字符串  
  
    // 将time_t转换为tm结构体，注意localtime返回的是指向静态内存的指针  
    std::tm* tm_info = std::localtime(&mtime);  
  
    // 使用strftime将tm结构体转换为字符串  
    // 注意：strftime的第二个参数是目标字符串的指针，第三个参数是目标字符串的大小  
    // 第四个参数是格式字符串，最后是指向tm结构体的指针  
    std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info);  
  

    std::stringstream ss;
    std::string str;
    // ss<< std::ctime(&mtime);  
    // std::cout<<std::ctime(&mtime)<<std::endl;

    // ss>>str;
    
    result = buffer;
  
    CloseHandle(hFile);  
    return result;  
}

#else
EString EFileInfo_getModifiedTimeStr(const char* path)
{
    EString result;

    struct stat fileStat;  

    // 使用stat函数获取文件状态  
    if (stat(path, &fileStat) == -1) 
    {  
        ELOG_ERROR("stat function return false");
        return result;  
    }  

    // 将时间戳转换为可读的格式  
    struct tm* timeInfo = localtime(&fileStat.st_mtime);  

    char timeBuffer[80];  
    strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", timeInfo);  
    // 打印文件的最后修改时间  
    // ELOG_INFO("File last modified time: %s", timeBuffer);  
    result = timeBuffer;

    return result;  
}

#endif

EFileInfo::EFileInfo(const EString& path)
{
    this->m_filePath = path;

}

EFileInfo::~EFileInfo()
{

}


EFileInfo::TYPE getType()
{
	return EFileInfo::FILE;
}


EString EFileInfo::getModifiedTimeStr()
{
    EString str = EFileInfo_getModifiedTimeStr(this->m_filePath.data());
    // ELOG_INFO("modified time=: %s", str.data());  
     
    return str;
}

EString EFileInfo::getBaseName()
{
    std::string path = this->m_filePath.data();
    EString basename = EString::fromStdString(pystring::os::path::basename( path));

    return basename;
}
